package ex2a.model.semaforos;

import java.util.Collection;

public class CustomSemaphore extends OpenSemaphore {
	private static final long serialVersionUID = 1L;
	private Class< ? extends Thread > priority = null;
	
	public CustomSemaphore( int permits, Class< ? extends Thread > priority, String name ) {
		super(permits, name);
		this.priority = priority;
	}
	
	public CustomSemaphore(int permits) {
		super(permits);
	}
	
	public CustomSemaphore( int permits, boolean fair, Class< ? extends Thread > priority ) {
		super( permits, fair );
		this.priority = priority;
	}
	
	public CustomSemaphore( int permits, boolean fair ) {
		super(permits, fair);
	}
	
	public void release() {
		Collection<Thread> ts = super.getQueuedThreads();
		
		// Dando prioridade para as Threads de Produtor...
		if( priority != null ) {
			
			for( Thread t : ts ) {
				if( t.getClass() == priority ) {
					t.interrupt();
					return;
				}
			}
		}
		super.release();
	}
}
